package com.wyl.salary.ui.page

import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.wyl.salary.R
import com.wyl.salary.ui.theme.SalaryTheme
import com.wyl.salary.ui.viewmodel.SetDurationViewModel

@Composable
fun SetDurationPage(
    startDate: String,
    endDate: String,
    onStartDateChange: (String) -> Unit,
    onEndDateChange: (String) -> Unit,
    onSaveDuration: () -> Unit,
    onRemoveDurationClick: () -> Unit,
    onBackPress: () -> Unit,
) {
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        topBar = {
            TopAppBar(
                title = { Text("设置起止时间") },
                navigationIcon = {
                    IconButton(onClick = onBackPress) {
                        Icon(imageVector = Icons.Filled.ArrowBack, contentDescription = "返回")
                    }
                }, actions = {
                    IconButton(onClick = onSaveDuration) {
                        Icon(imageVector = Icons.Filled.Done, contentDescription = "完成")
                    }
                }
            )
        },
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
        ) {
            OutlinedTextField(
                value = startDate,
                onValueChange = onStartDateChange,
                label = { Text(text = "开始日期") },
                modifier = Modifier.fillMaxWidth(),
                placeholder = { Text(text = "请输入开始日期") },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Phone,
                    imeAction = ImeAction.Next,
                ),
                singleLine = true
            )
            Spacer(modifier = Modifier.height(8.dp))
            OutlinedTextField(
                value = endDate,
                onValueChange = onEndDateChange,
                label = { Text(text = "结束日期") },
                modifier = Modifier.fillMaxWidth(),
                placeholder = { Text(text = "请输入结束日期") },
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Phone,
                    imeAction = ImeAction.Done,
                ),
                singleLine = true
            )
            Spacer(modifier = Modifier.height(16.dp))
            OutlinedButton(
                onClick = onRemoveDurationClick,
                modifier = Modifier.fillMaxWidth()
            ) {
                Text(text = "清除日期")
            }
        }
    }
}

@Composable
fun SetDurationPageWithViewModel(
    navController: NavHostController,
    viewModel: SetDurationViewModel = viewModel(),
    onDurationSave: () -> Unit,
) {
    val context = LocalContext.current
    SetDurationPage(
        startDate = viewModel.startDate,
        endDate = viewModel.endDate,
        onStartDateChange = viewModel::onStartDateChanged,
        onEndDateChange = viewModel::onEndDateChanged,
        onSaveDuration = {
            if (viewModel.save()) {
                navController.popBackStack()
                onDurationSave.invoke()
            } else {
                Toast.makeText(
                    context,
                    context.getString(R.string.date_error),
                    Toast.LENGTH_LONG
                ).show()
            }
        },
        onRemoveDurationClick = {
            viewModel.remove()
            navController.popBackStack()
        },
        onBackPress = navController::popBackStack,
    )
}

@Preview
@Composable
private fun PreviewSetDurationPage() {
    SalaryTheme {
        SetDurationPage(
            startDate = "2020-01-01",
            endDate = "2020-01-01",
            onStartDateChange = {},
            onEndDateChange = {},
            onSaveDuration = {},
            onBackPress = {},
            onRemoveDurationClick = {}
        )
    }
}